package com.tjise.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tjise.eduservice.entity.EduSubject;
import com.tjise.eduservice.entity.excel.ExcelSubjectData;
import com.tjise.eduservice.entity.subject.OneSubject;
import com.tjise.eduservice.entity.subject.TwoSubject;
import com.tjise.eduservice.listener.SubjectExcelListener;
import com.tjise.eduservice.mapper.EduSubjectMapper;
import com.tjise.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author TJISE
 * @since 2021-06-23
 */
@Service
@Slf4j
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {


    /**
     * 添加课程分类
     *
     * @param multipartFile
     * @param eduSubjectService
     */
    @Override
    public void saveSubject(MultipartFile multipartFile, EduSubjectService eduSubjectService) {
        try {
            // 文件的输入流
            InputStream inputStream = multipartFile.getInputStream();
            // 调用方法进行读取
            EasyExcel.read(inputStream, ExcelSubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 课程分类的列表
     *
     * @return
     */
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        // 查询所有的一级分类 parent_id = 0
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        // 判断条件是parent_id 这个字段的值为0
        wrapperOne.eq("parent_id", "0");
        // 查询所有的一级分类
        List<EduSubject> eduSubjectOne = baseMapper.selectList(wrapperOne);


        // 查询出所有的二级分类 parent_id != 0
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        // 判断条件是parent_id 这个字段的值不为0
        wrapperTwo.ne("parent_id", "0");
        List<EduSubject> eduSubjectsTwo = baseMapper.selectList(wrapperTwo);

        // 创建一个list集合用来存储最终封装的数据
        List<OneSubject> finalSubject = new ArrayList<>();
        // 分装一级分类
        for (EduSubject eduSubject : eduSubjectOne) {
            log.info("EduSubjectServiceImpl------------------>中的eduSubjectOne中的每个eduSubject" + eduSubject);
            // 把 eduSubject 里面的值取出来将他放到OneSubject中
            OneSubject oneSubject = new OneSubject();
            // oneSubject.setId(eduSubject.getId());
            // oneSubject.setTitle(eduSubject.getTitle());
            // 使用工具类传入到  oneSubject 中 进行封装
            BeanUtils.copyProperties(eduSubject, oneSubject);
            // 将数据放入到 finalSubject 集合中
            finalSubject.add(oneSubject);
            // 分装二级分类
            // 创建list的集合封装每个一级分类的二级分类
            List<TwoSubject> twoFinalSubjectList = new ArrayList<>();
            for (EduSubject tSubject : eduSubjectsTwo) {
                // 获取每个二级分类
                log.info("EduSubjectServiceImpl------------------>中的eduSubjectsTwo中的每个(二级分类)subject" + tSubject);
                // 判断二级分类的parent_id和一级分类的id是否相同
                if (Objects.equals(tSubject.getParentId(), eduSubject.getId())) {
                    // 把 twoSubject 中的值复制到tSubject中
                    TwoSubject twoSubject = new TwoSubject();
                    // 使用工具类进行赋值属性
                    BeanUtils.copyProperties(tSubject, twoSubject);
                    twoFinalSubjectList.add(twoSubject);
                }
            }
            // 把一级分类下所有的二级分类放入到一级分类里面
            oneSubject.setChildren(twoFinalSubjectList);
        }
        return finalSubject;
    }
}
